package com.dg.river.module.common.adapter;

import androidx.viewpager.widget.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dg.river.R;
import com.dg.river.core.util.GlideShowUtils;
import com.bumptech.glide.RequestManager;
import com.luck.picture.lib.photoview.OnOutsidePhotoTapListener;
import com.luck.picture.lib.photoview.OnPhotoTapListener;
import com.luck.picture.lib.photoview.PhotoView;

import java.util.List;

/**
 *
 */
public class SamplePagerAdapter extends PagerAdapter {

    private List<String> image;
    /**
     * 点击事件
     */
    private OnMyItemClickListener mOnMyItemClickListener;

    private RequestManager glide;

    /**
     * 设置点击事件
     *
     * @param mOnMyItemClickListener
     */
    public void setOnItemClickLitener(OnMyItemClickListener mOnMyItemClickListener) {
        this.mOnMyItemClickListener = mOnMyItemClickListener;
    }

    /**
     * ItemClick的回调接口
     */
    public interface OnMyItemClickListener {
        void onItemClick(View view);
    }

    public SamplePagerAdapter(List<String> image, RequestManager glide) {
        this.image = image;
        this.glide = glide;
    }

    @Override
    public int getCount() {
        return image.size();
    }

    @Override
    public View instantiateItem(final ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(container.getContext());

        GlideShowUtils.GlidePicture(glide, image.get(position), photoView, R.mipmap.ic_launcher, false);

        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView view, float x, float y) {
                mOnMyItemClickListener.onItemClick(view);
            }
        });

        photoView.setOnOutsidePhotoTapListener(new OnOutsidePhotoTapListener() {
            @Override
            public void onOutsidePhotoTap(ImageView imageView) {
                mOnMyItemClickListener.onItemClick(imageView);
            }
        });

        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


}
